package com.budwk.app.sqfwV2.generator.dayingRoadExcel;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.common.component.DictComponent;
import com.budwk.app.sqfw.constants.DictConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.component.ProblemComponent;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.ProblemParamDTO;
import com.budwk.app.sqfwV2.dto.image.FireSafetyOnDayingRoadExcelImageConfigBO;
import com.budwk.app.sqfwV2.enums.DayingRoadFileServiceEnum;
import com.budwk.app.sqfwV2.enums.dayingRoad.OtherPatrolContentsEnum;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 10 附件3：夜市摊点、游摊燃气台账/Excel
 */

@Slf4j
public class NightStallBookGenerator extends BaseTemplateExcelGenerator implements DayingRoadExcelGenerator {

    @Override
    protected String getTemplateName() {
        return DayingRoadFileServiceEnum.DAYING_ROAD_EXCEL_STALLS.getFilePath();
    }

    /**
    * 可以参照 {@link com.budwk.app.sqfw.enums.DownLoadFileNameEnum}
    *
    * @return 文件名名称
    */
    @Override
    public String getDownloadFileName() {
        //TODO 在这里返回你生成的文件名名称，注意注意注意不用带上后缀
        return DayingRoadPdfComponent.getDictName(DayingRoadFileServiceEnum.DAYING_ROAD_EXCEL_STALLS.getCode(), null);
    }

    /**
     * 用户自定义excel基础参数，比如表头这样的公用数据
     * @param excelParam 已经填充的基础数据据
     * @param excelCreateParam excel生成提供的原料
     */
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {
        excelParam.put("street", excelCreateParam.getBaseExcelParamDTO().getCounty());
        SysUserService userService = NutzIocUtils.getBean(SysUserService.class);
        Sys_user user = userService.fetch(excelCreateParam.getBaseExcelParamDTO().getCreateUserId());
        excelParam.put("createUsernamePhone", excelCreateParam.getBaseExcelParamDTO().getCreateUserName() + " " + user.getMobile());
        excelParam.put("downloadDateStr", DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN));
        excelParam.put("unitName", DayingRoadPdfComponent.getUnitName(excelCreateParam.getBaseExcelParamDTO()));
        excelParam.put("district", DayingRoadPdfComponent.getDistrict(excelCreateParam.getBaseExcelParamDTO().getDistrict()));
    }

    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {
        List<Sqfw_formSummary_new> summaryList = DayingRoadPdfComponent.getSummaryList(listMap, excelCreateParam);
        log.info("NightStallBookGenerator customerPopularSheetDataParam summaryList: {}", JSONObject.toJSONString(summaryList));
        int num = 1;
        for (Sqfw_formSummary_new summary : summaryList) {
            FireSafetyOnDayingRoadPdfDTO roadPdfDTO = JSONObject.parseObject(summary.getParamData(), FireSafetyOnDayingRoadPdfDTO.class);
            log.info("NightStallBookGenerator customerPopularSheetDataParam roadPdfDTO: {}", JSONObject.toJSONString(roadPdfDTO));
            if (!isGenerateExcel(roadPdfDTO)) {
                continue;
            }
            // 构建数据
            Map<String, Object> map = buildExcelMap(roadPdfDTO);
            map.put("num", num ++);
            listMap.add(map);
        }
        log.info("NightStallBookGenerator customerPopularSheetDataParam end: {}", JSONObject.toJSONString(listMap));
    }

    private Map<String, Object> buildExcelMap(FireSafetyOnDayingRoadPdfDTO roadPdfDTO) {
        Map<String, Object> map = new HashMap<>();
        map.put("placeName", StrUtil.replaceNullStr(roadPdfDTO.getPlaceName()));
        map.put("address", StrUtil.replaceNullStr(roadPdfDTO.getAddress()));
        map.put("operator", StrUtil.replaceNullStr(roadPdfDTO.getOperator()));
        map.put("operatorTelephone", StrUtil.replaceNullStr(roadPdfDTO.getOperatorTelephone()));
        map.put("marketStorageAddress", StrUtil.replaceNullStr(roadPdfDTO.getMarketStorageAddress()));
        map.put("liquefiedGasNumber", StrUtil.replaceNullStr(roadPdfDTO.getLiquefiedGasNumber()));
        map.put("socialCreditCodeStr", StringUtils.isNotEmpty(roadPdfDTO.getSocialCreditCode()) ? "有执照" : "无执照"); // 执照情况(针对夜市摊点)
        // 燃气存储地是否合规(含收市后存放地) 1 2 3 8 问题现场整改和期限整改

        ProblemParamDTO ppd = new ProblemParamDTO(); // 整改情况
        ppd.setRemark("-");
        map.put("gasStores", ProblemComponent.getGasStoreStr(roadPdfDTO, ppd));
        String gasDeliveryCompany = roadPdfDTO.getGasDeliveryCompany();
        String dictName = DayingRoadPdfComponent.getDictName(gasDeliveryCompany, null);
        map.put("gasDeliveryCompany", dictName); // 供气企业名称
        //  钢瓶、管道、灶具等燃气用户状况  4 5 7 17 18 问题
        map.put("gasStovesStr", ProblemComponent.getGasStovesStr(roadPdfDTO, ppd)); // 钢瓶、管道、灶具等燃气用户状况
        // 消防安全设施储备情况  14 16
        map.put("reserveFireSafety", ProblemComponent.getReserveFireSafety(roadPdfDTO, ppd));
        // 检查1-25个问题是否有异常
        map.put("safeDesc", ProblemComponent.getSafeDescStr(roadPdfDTO, ppd));
        map.put("beenRectified", StrUtil.replaceNullStr(ppd.getRemark())); // 是否已整改
        return map;
    }

    /**
     * 是否生成数据
     * ●生成表格注意事项：
     * ○【用户用火用电情况】状态为【瓶装液化气】或【瓶装天然气】或【瓶装二甲醚】或【管道燃气】或【使用醇基燃料】的状态外且【a13】状态为【是】，生成表格。
     * @param roadPdfDTO
     * @return
     */
    public boolean isGenerateExcel(FireSafetyOnDayingRoadPdfDTO roadPdfDTO) {
        // 判断a5是否为重点场所 选择重点场所 不生成表格
        if (DayingRoadPdfComponent.isKeyLocations(roadPdfDTO)) {
            return false;
        }
        // 1 【用户用火用电情况】状态为【瓶装液化气】或【瓶装天然气】或【瓶装二甲醚】或【管道燃气】或【使用醇基燃料】的状态外且【a13】状态为【是】，生成表格。
        String fireAndElectricityUsage = roadPdfDTO.getFireAndElectricityUsage();
        if (StringUtils.isEmpty(fireAndElectricityUsage)) {
            return false;
        }
        // 组装【瓶装液化气】或【瓶装天然气】或【瓶装二甲醚】或【管道燃气】或【使用醇基燃料】数组
        List<String> codes = DictComponent.listFireGasCode();
        codes.add(DictConstants.BOTTLED_ETHER_ELECTRICITY_GAS);
        codes.add(DictConstants.BOTTLED_GAS_FUELS);
        long count = codes.stream().filter(fireAndElectricityUsage::contains).count();
        if (count == 0) {
            return false;
        }
        // 【a13】状态为【是】 是否为夜市游摊
        Boolean isNightMarketStalls = roadPdfDTO.getIsNightMarketStalls();
        return isNightMarketStalls != null && isNightMarketStalls;
    }

    /**
     * ●生成表格注意事项：
     * ○【用户用火用电情况】状态为【瓶装液化气】或【瓶装天然气】或【瓶装二甲醚】或【管道燃气】或【使用醇基燃料】的状态外且【a13】状态为【是】，生成表格。
     * @param excelFileCreateParam 已经设置过的一些参数
     */
    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {

    }


    /**
     * 如果在遍历之前就能够判断excel是否生成，那么就在这个方法中进行判断
     * @param excelCreateParam excel生成所需的最为全面的数据
     * @param excelParam excel已经填充的部分数据
     */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        return DayingRoadPdfComponent.getSqfwExcelJudgeRes(excelCreateParam, new NightStallBookGenerator());
    }


    /**
    * 如果在遍历之后能够判断excel是否生成，那么就在这个方法中进行判断
    * @param excelParam excel已经填充的部分数据
    */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return FireSafetyOnDayingRoadExcelImageConfigBO.class;
    }
}
